package main;

import service.*;
import view.GamePanelView;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;

/**
 * 游戏面板类
 */
public class GamePanel extends JPanel implements KeyListener {
    // -------------------- 数据设计 --------------------
    private Timer gameTimer;
    private Timer enemyCreateTimer;

    private GameLogicService gameLogicService;
    private InputService inputService;
    private GamePanelView gamePanelView;

    private BufferedImage bufferedImage;
    private Graphics bufferedGraphics;
    // -------------------- 数据设计 --------------------

    /**
     * 功能: 游戏面板构造函数
     * 初始化面板组件和服务
     * 参数: void
     * 返回值: void
     */
    public GamePanel() {
        initializePanel();
        createServices();
        startGameLoop();
        startEnemyCreateLoop();
    }

    /**
     * 功能: 初始化面板
     * 参数: void
     * 返回值: void
     */
    private void initializePanel() {
        setPreferredSize(new Dimension(1200, 900));
        setBackground(Color.BLACK);
        setFocusable(true);
        addKeyListener(this);

        // 初始化双缓冲
        bufferedImage = new BufferedImage(1200, 900, BufferedImage.TYPE_INT_RGB);
        bufferedGraphics = bufferedImage.getGraphics();
    }

    /**
     * 功能: 创建服务层对象
     * 参数: void
     * 返回值: void
     */
    private void createServices() {
        gameLogicService = new GameLogicService();
        inputService = new InputService();
        gamePanelView = new GamePanelView();
    }

    /**
     * 功能: 启动游戏主循环
     * 参数: void
     * 返回值: void
     */
    private void startGameLoop() {
        // 60FPS游戏循环
        gameTimer = new Timer(16, e -> {
            gameLogicService.update();
            repaint();
        });
        gameTimer.start();
    }

    /**
     * 功能: 启动敌方坦克创建循环
     * 参数: void
     * 返回值: void
     */
    private void startEnemyCreateLoop() {
        // 每3秒尝试创建敌方坦克
        enemyCreateTimer = new Timer(3000, e -> {
            if (data.GameRuntimeData.getInstance().isGameRunning()) {
                gameLogicService.createEnemyTank();
            }
        });
        enemyCreateTimer.start();
    }

    /**
     * 功能: 绘制游戏画面
     * 参数:
     *   g: Graphics绘图对象
     * 返回值: void
     */
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        // 使用双缓冲绘制
        gamePanelView.drawGamePanel(bufferedGraphics);
        g.drawImage(bufferedImage, 0, 0, null);
    }

    /**
     * 负责人: 阿呆
     * 功能: 键盘按下事件处理
     * 参数:
     *   e: 键盘事件
     * 返回值: void
     */
    @Override
    public void keyPressed(KeyEvent e) {
        int keyCode = e.getKeyCode();

        // 处理游戏控制键
        if (keyCode == KeyEvent.VK_P) {
            // 暂停/继续
            data.GameRuntimeData runtimeData = data.GameRuntimeData.getInstance();
            if (runtimeData.getGameState() == 1) {
                runtimeData.setGameState(2);
                runtimeData.setGameRunning(false);
            } else if (runtimeData.getGameState() == 2) {
                runtimeData.setGameState(1);
                runtimeData.setGameRunning(true);
            }
        } else if (keyCode == KeyEvent.VK_R) {
            // 重新开始
            gameLogicService.startNewGame();
        } else if (keyCode == KeyEvent.VK_SPACE &&
                data.GameRuntimeData.getInstance().getGameState() == 0) {
            // 开始游戏
            gameLogicService.startNewGame();
        } else {
            // 处理坦克移动和射击
            inputService.handleKeyPressed(keyCode);
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
        // 暂不处理键盘释放事件
    }

    @Override
    public void keyTyped(KeyEvent e) {
        // 暂不处理键盘输入事件
    }
}